<template>
  <a-modal :maskClosable="false"
    :title='title'
    :width='600'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'
  >
    <a-spin :spinning='confirmLoading'>
      <a-form-model ref='form' v-bind='layout' :model='model'>
        <a-form-model-item label='配套所属类别' required prop='projectType'>
          <a-select v-model='model.projectType' placeholder='请选择类型'>
            <a-select-option value='1'>风险等级</a-select-option>
            <a-select-option value='2'>体质类型</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label='风险等级' required prop='busValue' v-if="model.projectType=='1'">
          <j-dict-select-tag placeholder='请选择风险等级' dictCode='risk_level' v-model='model.busValue' />
        </a-form-model-item>
        <a-form-model-item label='体质类型' required prop='busValue' v-if="model.projectType=='2'">
          <a-select placeholder='' v-model='model.busValue'>
            <a-select-option value=''>请选择</a-select-option>
            <a-select-option v-for='item in selectConstitution' :key='item.key' :value='item.key'>{{ item.label }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label='配套类型' required prop='packageType'>
          <a-select v-model='model.packageType' placeholder='请选择类型' @change='onChangeType'>
            <a-select-option value='1'>套餐方案</a-select-option>
            <a-select-option value='2'>膳食方案</a-select-option>
            <a-select-option value='3'>运动方案</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label='配套服务' required prop='packageId'>
          <a-select placeholder='' v-model='model.packageId'>
            <a-select-option value=''>请选择</a-select-option>
            <a-select-option v-for='item in serviceList' :key='item.key' :value='item.key'>{{ item.label }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addDict, editDict, duplicateCheck } from '@api/api'
import { getAction, httpAction } from '@api/manage'

export default {
  name: 'DictModal',
  components: {},
  data() {
    return {
      title: '操作',
      visible: false,
      model: {
        projectType: '1',
        risk: '',
        packageType: '1',
        busValue:"",
        packageId:"",
      },
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        busValue: [{ required: true, message: '请选择风险等级!' }],
        packageId: [{ required: true, message: '请选择配套服务!' }]
      },
      selectConstitution: [],
      url: {
        add: '/ch/evaluation/package',
        edit: '/ch/evaluation/package',
        selectList: '/ch/door/project/selectList',
        ordSelectList: '/ord/ordCpInfo/selectList',
        sportSelectList: '/ch/sport/plan/selectList',
        selectConstitution: '/ch/evaluation/package/selectConstitution'

      },
      serviceList: []
    }
  },
  created() {
    getAction(this.url.selectList, { 'projectType': '5' }).then((res) => {
      if (res.success) {
        this.serviceList = res.result
      }
    })
    getAction(this.url.selectConstitution).then((res) => {
      if (res.success) {
        this.selectConstitution = res.result
      }
    })
  },
  methods: {
    add() {
      this.model.projectType = "1"
      this.model.packageType = "1"
      this.model.busValue = ''
      this.model.packageId = ''
      this.visible = true
    },
    edit(record) {
      this.model.id = record.id
      this.model.risk = record.risk
      this.visible = true
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          var patientId = this.$route.query.patientId
          this.model.patientId = patientId
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    onChangeType(value) {
      if ('1' == value) {
        getAction(this.url.selectList, { 'projectType': '5' }).then((res) => {
          if (res.success) {
            this.serviceList = res.result
          }
        })
      } else if ('2' == value) {
        getAction(this.url.ordSelectList).then((res) => {
          if (res.success) {
            this.serviceList = res.result
          }
        })
      } else if ('3' == value) {
        getAction(this.url.sportSelectList).then((res) => {
          if (res.success) {
            this.serviceList = res.result
          }
        })
      }
    }
  }
}
</script>